package com.learn.springboot3;



import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * ClassName: HttpPost
 * Package: com.learn.springboot3
 * Description:
 *
 * @Author jiangCheng
 * @Create 2024/10/12 10:04
 * @Version 1.0
 */
public class HttpPost {

    public static void main(String[] args) throws IOException {

        int page =1;

        int pageSize = 10;
        String json = "{"
                + "    \"status\": \"All\","
                + "    \"page\": "+page+","
                + "    \"pageSize\": "+pageSize
                + "}";
        String result = post("http://httpbin.org/post", json);
        ObjectMapper mapper = new ObjectMapper();
        PanelLog panelLog = mapper.readValue(result, PanelLog.class);
        FileOutputStream os = new FileOutputStream("D:\\资料\\1Panel2.txt");



        List<LogsBean> logs = panelLog.getData().getLogs();
        int totalCount = panelLog.getData().getTotalCount();


        pageSize =100;
        for (int i = 0; i <=totalCount/pageSize; i++) {

            json ="{"
                    + "    \"status\": \"All\","
                    + "    \"page\": "+(i+1)+","
                    + "    \"pageSize\": "+pageSize
                    + "}";

             result = post("http://httpbin.org/post", json);
            ObjectMapper mapper1 = new ObjectMapper();
            PanelLog panelLog1 = mapper1.readValue(result, PanelLog.class);
            List<LogsBean> logs1 = panelLog1.getData().getLogs();
            for (LogsBean log : logs1) {
                if (log.getStatus().equals("Failed")){
                    System.out.println(log.getAddress());
                    os.write(log.getAddress().getBytes());
                    os.write(",\r\n".getBytes());
                }

            }
        }





        System.out.println(result);
    }

    public static String post(String url, String jsonBody) {
        String result = null;


        org.apache.hc.client5.http.classic.methods.HttpPost post = new org.apache.hc.client5.http.classic.methods.HttpPost("http://111.229.34.136:36648/api/v1/hosts/ssh/log");
        post.setHeader("Content-Type", "application/json;charset=UTF-8");
        post.setHeader("Accept", "application/json, text/plain, */*");
        post.setHeader("Cookie", "device_id=fb738a4586354c63b714aaccaf458022; iconSize=24x24; SESS_COOKIE_KUBEPI=0bef2b2e-28a4-41f7-bb02-211f3941cf80; kodUserLanguage=zh-CN; zfile-token=e224e6d1-4743-4087-bdf2-832131b30a24; remember-me=amlhbmdjaGVuZzoxNzI5NjYxOTg5NDk2OjhiMjEyMGE5ZmNkNWFhYThlMDgxNTdmY2JmYWYzN2E0MzczNWE3NDJjODBmOGJjNjlmNmY2MzMzNjI4YWJkN2M; psession=adbd935f-cbd6-469c-b3bb-69b3ec89af9f");
        post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0");
        post.setEntity(new StringEntity(jsonBody));
        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            try (CloseableHttpResponse response = httpclient.execute(post)) {
                // 获取响应信息
                result = EntityUtils.toString(response.getEntity());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
